package pe.edu.upla.lamerced.DAO.impl;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.springframework.stereotype.Repository;

import pe.edu.upla.lamerced.DAO.CategoriaDAO;
import pe.edu.upla.lamerced.bean.Categoria;

@Repository
public class CategoriaDAOImpl extends HibernateDaoSupport implements CategoriaDAO{
	
	public CategoriaDAOImpl(){}
	
	@Autowired
	public CategoriaDAOImpl(SessionFactory sessionFactory){
		setHibernateTemplate( new HibernateTemplate(sessionFactory));
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public List<Categoria> listarCategorias() {
		return this.getHibernateTemplate().find("from Categoria c");
	}

	@Override
	public Categoria obtenercategoria(Integer cat) {
		Query query = getSession().createQuery(" from Categoria h where idcategoria= :id ")
        .setInteger("id", cat);
        return (Categoria) query.uniqueResult();

	}

	@Override
	public void registrarCategoria(Categoria categoria) {
		this.getHibernateTemplate().saveOrUpdate(categoria);
	}


	}
